﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Globalization;
using Libs.HR;

public partial class Pages_Hr_Contracts_Add : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        AppUtils.CheckLogin();
        Title = Title + " - Thêm mới hợp đồng";
        if (!IsPostBack)
        {
            init();
        }
    }

    private void init()
    {
        var _Employee = new Employees() { EmployeeID = AppUtils.Request("id") };
        _Employee = _Employee.Get();
        if (_Employee == null) Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesList);

        txtFullName.Text = _Employee.LastName + " " + _Employee.FirstName;
        txtBirthday.Text = _Employee.Birthday.ToString("dd/MM/yyyy");

        // Loại hợp đồng
        var _ContractType = new ContractType();
        drpContractType.DataSource = _ContractType.GetTList();
        drpContractType.DataBind();
        drpContractType.Items.Insert(0, new ListItem("Chọn loại hợp đồng:", "0"));

        // Công ty
        var _Department = new Departments();
        drpCompany.DataSource = _Department.GetTList(0);
        drpCompany.DataBind();
    }

    protected void btAdd_Click(object sender, EventArgs e)
    {
        var _Contract = new Contracts();
        _Contract.EmployeeID = AppUtils.Request("id");
        _Contract.TypeID = Convert.ToInt32(drpContractType.SelectedValue);
        _Contract.ContractNumber = txtContractNumber.Text;
        _Contract.SignedTime = Convert.ToDateTime(txtSignedTime.Text, new CultureInfo("vi-VN"));
        _Contract.BeginTime = Convert.ToDateTime(txtBeginTime.Text, new CultureInfo("vi-VN"));
        _Contract.EndTime = txtEndTime.Text.Trim().Length == 0 ? DateTime.MaxValue : Convert.ToDateTime(txtEndTime.Text, new CultureInfo("vi-VN"));
        _Contract.IsFinish = cbIsFinish.Checked;
        _Contract.Description = txtDescription.Text.Trim();
        _Contract.Add();

        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesEdit + "?id=" + _Contract.EmployeeID.ToString());
    }

    protected void btCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Constant.ADMIN_PATH + Resources.Url.EmployeesEdit + "?id=" + Request["id"]);
    }
}
